layui.use(function () {
  var table = layui.table;
  var form = layui.form;
  var laydate = layui.laydate;
  var type = '1';

  laydate.render({
    elem: '#search-date',
    range: true, //或 range: '~' 来自定义分割字符
  });

  setTimeout(() => {
    $('.search-bar > button')[0].click();
  }, 100);

  form.on('submit(search)', function (data) {
    var params = {};
    var url;
    var col = [];
    switch (type) {
      case '1':
        url = '/data/sms/list/history';
        params.beginDate = data.field.date.substring(0, 10);
        params.endDate = data.field.date.substring(13, 24);
        params.phone = data.field.phone;
        col = [
          {
            field: 'phoneNumber',
            title: '手机号',
            align: 'center',
          },
          {
            field: 'deviceName',
            title: '设备名称',
            align: 'center',
          },
          {
            field: 'deviceCode',
            title: '设备号',
            align: 'center',
          },
          {
            field: 'content',
            title: '短信内容',
            align: 'center',
            width: 700,
          },
          {
            field: 'sendDesc',
            title: '状态',
            align: 'center',
          },
          {
            field: 'dataTime',
            title: '发送时间',
            align: 'center',
          },
        ];
        break;
      case '2':
        url = '/data/vms/list/history';
        params.beginDate = data.field.date.substring(0, 10);
        params.endDate = data.field.date.substring(13, 24);
        params.phone = data.field.phone;
        col = [
          {
            field: 'phoneNumber',
            title: '手机号',
            align: 'center',
          },
          {
            field: 'deviceName',
            title: '设备名称',
            align: 'center',
          },
          {
            field: 'deviceCode',
            title: '设备号',
            align: 'center',
          },
          {
            field: 'content',
            title: '语音内容',
            align: 'center',
            width: 500,
          },
          {
            field: 'stateDesc',
            title: '状态',
            align: 'center',
          },
          {
            field: 'dataTime',
            title: '发送时间',
            align: 'center',
          },
        ];
        break;
      case '3':
        url = '/operate/list';
        params.beginTime = data.field.date.substring(0, 10);
        params.endTime = data.field.date.substring(13, 24);
        col = [
          {
            field: 'operateModelDesc',
            title: '操作模块',
            align: 'center',
          },
          {
            field: 'operateTypeDesc',
            title: '操作类型',
            align: 'center',
          },
          {
            field: 'operateUserName',
            title: '操作人',
            align: 'center',
          },
          {
            field: 'operateTime',
            title: '操作时间',
            align: 'center',
          },
        ];
        break;
    }
    initTable(url, params, col);
    return false;
  });

  function initTable(url, params, col) {
    var height = $('.contain').innerHeight() - 36 - 98 - 26;

    //第一个实例
    table.render({
      title: '',
      height: height,
      text: {
        none: '暂无数据',
      },
      elem: '#table',
      skin: 'nob',
      headers: {
        SessionId: JSON.parse(localStorage.getItem('userInfo')).sessionId,
      },
      url: ajaxPrefix() + url,
      where: params,
      parseData: function (res) {
        //res 即为原始返回的数据
        return {
          code: res.status == 200 ? 0 : 1, //解析接口状态
          msg: res.message, //解析提示文本
          count: res.result?res.result.total:0, //解析数据长度
          data: res.result?res.result.data:[], //解析数据列表
        };
      },
      request: {
        pageName: 'pageNum', //页码的参数名称，默认：page
        limitName: 'pageSize', //每页数据量的参数名，默认：limit
      },
      page: {
        //开启分页
        limit: 10,
      },
      cols: [col],
    });
  }

  $('.tab-item').click(function (e) {
    $(e.currentTarget).addClass('active').siblings().removeClass('active');
    type = $(this)[0].dataset.type;
    if (type == '3') {
      $('#phone-block').hide();
    } else {
      $('#phone-block').show();
    }
    $('.search-bar > button')[0].click();
  });
});
